package logic

import (
	"context"
	"fmt"
	"inventory-system/model/mysql"
	"inventory-system/pkg"

	"inventory-system/systemrpc/internal/svc"
	"inventory-system/systemrpc/systemrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *systemrpc.RegisterRequest) (*systemrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var m mysql.User
	mysql.Db.Debug().Where("account=?", in.Account).Find(&m)
	if m.ID != 0 {
		return nil, fmt.Errorf("账号已存在")
	}
	user := mysql.User{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	mysql.Db.Debug().Create(&user)

	return &systemrpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
